package com.cskaoyan.service.mall;


import com.cskaoyan.bean.BaseParam;

import com.cskaoyan.bean.PageDataVo;
import com.cskaoyan.bean.mall.aftersale.AfterSale;
import com.cskaoyan.bean.mall.aftersale.AfterSaleExample;
import com.cskaoyan.bean.mall.aftersale.AfterSaleStatus;

import com.cskaoyan.bean.mall.order.Order;
import com.cskaoyan.mapper.mall.AfterSaleMapper;
import com.cskaoyan.mapper.mall.OrderMapper;
import com.cskaoyan.utils.MarketPageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @description:
 * @author: enhui
 **/
@Service
public class AfterSaleServiceImpl implements AfterSaleService {
    @Autowired
    AfterSaleMapper afterSaleMapper;

    @Autowired
    OrderMapper orderMapper;

    @Override
    public PageDataVo afterSaleList(Short status, Integer id, String aftersaleSn, BaseParam param) {
        MarketPageUtil.startPage(param);
        AfterSaleExample afterSaleExample = new AfterSaleExample();
        AfterSaleExample.Criteria criteria = afterSaleExample.createCriteria();
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (!StringUtils.isEmpty(aftersaleSn)) {
            criteria.andAftersaleSnEqualTo(aftersaleSn);
        }
        afterSaleExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<AfterSale> afterSales = afterSaleMapper.selectByExample(afterSaleExample);
        return PageDataVo.ok(afterSales);
    }

//    @Override
//    public AfterSale receive(AfterSale afterSale) {
//        Date date = new Date();
//        afterSale.setUpdateTime(date);
//        afterSale.setHandleTime(date);
//        afterSale.setStatus(AfterSaleStatus.RECEIVED.getCode());
//        //更新
//        int count = afterSaleMapper.updateByPrimaryKeySelective(afterSale);
//        //查询并返回
//        if (count == 1) {
//            return afterSaleMapper.selectByPrimaryKey(afterSale.getId());
//        }
//        return null;
//    }

    @Override
    public AfterSale update(AfterSale afterSale, AfterSaleStatus afterSaleStatus) {
        Date date = new Date();
        afterSale.setUpdateTime(date);
        afterSale.setHandleTime(date);
        afterSale.setStatus(afterSaleStatus.getCode());
        //更新
        int count = afterSaleMapper.updateByPrimaryKeySelective(afterSale);
        //根据order_id改order表的售后状态
        Order order = new Order();
        order.setAftersaleStatus(afterSaleStatus.getCode());
        order.setUpdateTime(date);
        order.setId(afterSale.getOrderId());
        int orderCount = orderMapper.updateByPrimaryKeySelective(order);
        //查询并返回
        if (count == 1) {
            return afterSaleMapper.selectByPrimaryKey(afterSale.getId());
        }
        return null;
    }

    @Override
    public List<AfterSale> batchUpdate(List<Integer> ids, AfterSaleStatus afterSaleStatus) {
        //用于返回的list，可以不返回
        List<AfterSale> afterSaleList = new ArrayList<>();
        //先根据id查，只update状态status=1的，并查询update后的数据，加入List
        for (Integer id : ids) {
            AfterSale afterSale = afterSaleMapper.selectByPrimaryKey(id);
            if(afterSale.getStatus().equals(AfterSaleStatus.APPLIED.getCode())){
                //update成record
                AfterSale record = new AfterSale();
                Date date = new Date();
                record.setUpdateTime(date);
                record.setHandleTime(date);
                record.setStatus(afterSaleStatus.getCode());
                record.setId(id);
                //根据id update 售后表的 afterSaleStatus和时间
                int count = afterSaleMapper.updateByPrimaryKeySelective(record);
                //根据order_id update 订单表的 afterSaleStatus和时间
                Order order = new Order();
                order.setAftersaleStatus(afterSaleStatus.getCode());
                order.setUpdateTime(date);
                order.setId(afterSale.getOrderId());
                int orderCount = orderMapper.updateByPrimaryKeySelective(order);
                if (count == 1) {
                    //查询更改后的afterSale，加入需要返回的list中
                    AfterSale afterSaleUpdate = afterSaleMapper.selectByPrimaryKey(id);
                    afterSaleList.add(afterSaleUpdate);
                }
            }
        }
        return afterSaleList;
    }
}
